﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_DocGia_Lop : DevExpress.XtraEditors.XtraForm
    {
        public frm_DocGia_Lop()
        {
            InitializeComponent();
        }
        public void GetLoad(int load)
        {
            txt_Load.Text = load.ToString();
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        Lop_BLL lop_bll = new Lop_BLL();
        private void frm_DocGia_Lop_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = lop_bll.LayDuLieu();
            txt_Load.Hide();
        }
        private void btn_Them_Click(object sender, EventArgs e)
        {
            frm_DocGia_Lop_Them f = new frm_DocGia_Lop_Them();
            f.Flag = 0;
            f.GetLoad = new frm_DocGia_Lop_Them.GetInt(GetLoad);
            f.ShowDialog();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            frm_DocGia_Lop_Them f = new frm_DocGia_Lop_Them();
            f.Flag = 1;
            f.GetLoad = new frm_DocGia_Lop_Them.GetInt(GetLoad);
            f.GetLoad = new frm_DocGia_Lop_Them.GetInt(GetLoad);
            f.MaLop = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Lớp").ToString());
            f.TenLop =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Lớp").ToString();
            f.TenDonVi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            f.KhoaHoc =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Khóa Học").ToString();
            f.GhiChu =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            f.ShowDialog();
        }

        private void btn_Chon_Click(object sender, EventArgs e)
        {
            MyGetData(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Lớp")));
            this.Close();
        }
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            MyGetData(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Lớp")));
            this.Close();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            Lop_Public l=new Lop_Public();
            l.MaLop=Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Lớp").ToString());
            l.TenLop =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Lớp").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Lớp: '" + l.TenLop + "' có Mã là: '" + l.MaLop + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                try
                {
                    lop_bll.Delete_Lop(l);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Xóa";
                    nk.MoTa = "Xóa thông tin Lớp:'" + l.TenLop + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    frm_DocGia_Lop_Load(sender, e);
                }
                catch (Exception)
                {
                    XtraMessageBox.Show("Không thể xóa do có bản ghi ràng buộc dữ liệu với Lớp: '"+l.TenLop+"'","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_Load_TextChanged(object sender, EventArgs e)
        {
            if (txt_Load.Text == "1")
            {
                frm_DocGia_Lop_Load(sender,e);
                txt_Load.Text = "0";
            }
        }
    }
}